import 'package:flutter/material.dart';

class LoadingDialog {
  static var _showing = false;

  static show(BuildContext context) {
    if (!_showing){
      _showing = true;
      showDialog(
        context: context,
        barrierDismissible: false, // 点击遮罩不关闭
        barrierColor: Colors.transparent,
        builder:(_) => Center(
          child: SizedBox(
            width: 80,
            height: 80,
            child: DecoratedBox(
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius:BorderRadius.circular(8),
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withValues(alpha: 0.08),
                      blurRadius: 3,
                    )
                  ]
              ),
              child: Center(
                // flutter 布局机制有关 SizedBox 不强制约束child遵循尺寸；
                // CircularProgressIndicator 是个 BoxConstraints.loose 类型的组件会忽略SizedBox的约束
                // 必须搭配Center，Align，FittedBox, ConstrainedBox使用才可以（原理暂时不清楚）
                child: SizedBox(
                  width: 30,
                  height: 30,
                  child: CircularProgressIndicator(),
                ),
              ),
            ),
          ),
        ),
      ).then((_){
        //dialog关闭时触发
        _showing = false;
      });
    }

  }
  static hide(BuildContext context){
    if (_showing){
      Navigator.of(context, rootNavigator: true).pop();
    }
  }
}
